

/*
preferred habitat localization regressions:
robustness with respect to crisis measures, and dropping QE weeks

output:
- f3c_*.pdf: localization figures

*/


* plotting options
set scheme lean_uncluttered

* command line option
* set to 1 to re-estimate PH regressions (slow)
if "`1'"=="" {
	local estimate_phregs 0
}
else {
	local estimate_phregs `1'
}



********************************************************************************
********************************************************************************
if `estimate_phregs' == 1 {

* different crisis/non-crisis periods

use ../data/phreg_clean_auction_notesbonds_govpx_shock_all.dta, clear

************************
* high risk aversion period: R&R crisis measure
drop high_ra
gen yh = yh(year(auctionDate), halfyear(auctionDate))
format yh %th
merge m:1 yh using ../data/rr_us_fincrisis.dta, nogen keep(master match)
replace rr_crisis = 0 if missing(rr_crisis)

gen high_ra = rr_crisis>=1
sort close_time maturity_remaining cusip_govpx

* run PH regressions
n di "running regressions: RR crises"
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("rr1")

* semi-continuous RR crisis measure: between 0 and 1
* compress top of distribution
drop high_ra
gen high_ra = rr_crisis / 10
replace high_ra = 1 if high_ra>1
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("rr_cts")

************************
* high risk aversion period: HKM
gen ym = ym(year(auctionDate), month(auctionDate))
format yh %tm
merge m:1 ym using ../data/hkm_monthly.dta, nogen keep(master match)

drop high_ra
gen high_ra = icr_level<=0.055
sort close_time maturity_remaining cusip_govpx

n di "running regressions: HKM crises"
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("hkm_level")

* semi-continuous HKM crisis measure: between 0 and 1
drop high_ra
gen high_ra = -(icr_level - 0.075)
replace high_ra = 0 if high_ra<0
replace high_ra = high_ra / (0.04)
replace high_ra = 1 if high_ra > 1
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("hkm_cts")



************************
* drop QE weeks
use ../data/phreg_clean_auction_notesbonds_govpx_shock_all.dta, clear

* drop QE event dates (same week)
gen auction_week = dofw(auctionDate)
gen ind_qe_week = auction_week == dofw(mdy(11, 25, 2008)) | /// QE1
	auction_week == dofw(mdy(12, 1, 2008)) | /// QE1
	auction_week == dofw(mdy(12, 16, 2008)) | /// QE1
	auction_week == dofw(mdy(1, 28, 2009)) | /// QE1
	auction_week == dofw(mdy(3, 18, 2009)) | /// QE1
	auction_week == dofw(mdy(3, 18, 2009)) | /// QE1
	auction_week == dofw(mdy(9, 23, 2009)) | /// QE1
	auction_week == dofw(mdy(8, 10, 2010)) | /// QE2
	auction_week == dofw(mdy(9, 21, 2010)) | /// QE2
	auction_week == dofw(mdy(9, 21, 2012)) | /// QE3
	auction_week == dofw(mdy(5, 22, 2013)) | /// QE3
	auction_week == dofw(mdy(6, 19, 2013)) | /// QE3
	auction_week == dofw(mdy(7, 10, 2013)) | /// QE3
	auction_week == dofw(mdy(9, 18, 2013))
drop if ind_qe_week
sort close_time maturity_remaining cusip_govpx

* run PH regressions
n di "running regression"
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("no_qe_weeks")

}




********************************************************************************
********************************************************************************
* plot

plot_ph_regression, fname_stub("rr1") ///
	show_figs(0) figname("f3c_rr1") combine_figs(1)

plot_ph_regression, fname_stub("rr_cts") ///
	show_figs(0) figname("f3c_rr_cts") combine_figs(1)

plot_ph_regression, fname_stub("hkm_level") ///
	show_figs(0) figname("f3c_hkm_level") combine_figs(1)

plot_ph_regression, fname_stub("hkm_cts") ///
	show_figs(0) figname("f3c_hkm_cts") combine_figs(1)

plot_ph_regression, fname_stub("no_qe_weeks") ///
	show_figs(0) figname("f3c_no_qe_weeks") combine_figs(1)






